from pandas.io.sql import execute
from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession

from module_admin.entity.do.kuaile8_do import Kuaile8
from module_admin.entity.vo.kuaile8_vo import Kuaile8Model, Kuaile8PageQueryModel
from utils.page_util import PageUtil


class Kuaile8Dao:
    """
    kuaile8中奖号码查询模块数据库操作层
    """

    @classmethod
    async def get_kuaile8_detail_by_id(cls, db: AsyncSession, id: int):
        """
        根据获取kuaile8中奖号码查询详细信息

        :param db: orm对象
        :param id: 
        :return: kuaile8中奖号码查询信息对象
        """
        kuaile8_info = (
            (
                await db.execute(
                    select(Kuaile8)
                    .where(
                        Kuaile8.id == id
                    )
                )
            )
            .scalars()
            .first()
        )

        return kuaile8_info

    @classmethod
    async def get_kuaile8_detail_by_info(cls, db: AsyncSession, kuaile8: Kuaile8Model):
        """
        根据kuaile8中奖号码查询参数获取kuaile8中奖号码查询信息

        :param db: orm对象
        :param kuaile8: kuaile8中奖号码查询参数对象
        :return: kuaile8中奖号码查询信息对象
        """
        kuaile8_info = (
            (
                await db.execute(
                    select(Kuaile8).where(
                    )
                )
            )
            .scalars()
            .first()
        )

        return kuaile8_info

    @classmethod
    async def get_kuaile8_list(cls, db: AsyncSession, query_object: Kuaile8PageQueryModel, is_page: bool = False):
        """
        根据查询参数获取kuaile8中奖号码查询列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: kuaile8中奖号码查询列表信息对象
        """
        query = (
            select(Kuaile8)
            .where(
                Kuaile8.qi == query_object.qi if query_object.qi else True,
                Kuaile8.zj_number == query_object.zj_number if query_object.zj_number else True,
            )
            .order_by(Kuaile8.id.desc())
            .distinct()
        )
        kuaile8_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return kuaile8_list

    @classmethod
    async def add_kuaile8_dao(cls, db: AsyncSession, kuaile8: Kuaile8Model):
        """
        新增kuaile8中奖号码查询数据库操作

        :param db: orm对象
        :param kuaile8: kuaile8中奖号码查询对象
        :return:
        """
        db_kuaile8 = Kuaile8(**kuaile8.model_dump(exclude={}))
        db.add(db_kuaile8)
        await db.flush()

        return db_kuaile8

    @classmethod
    async def edit_kuaile8_dao(cls, db: AsyncSession, kuaile8: dict):
        """
        编辑kuaile8中奖号码查询数据库操作

        :param db: orm对象
        :param kuaile8: 需要更新的kuaile8中奖号码查询字典
        :return:
        """
        await db.execute(update(Kuaile8), [kuaile8])

    @classmethod
    async def delete_kuaile8_dao(cls, db: AsyncSession, kuaile8: Kuaile8Model):
        """
        删除kuaile8中奖号码查询数据库操作

        :param db: orm对象
        :param kuaile8: kuaile8中奖号码查询对象
        :return:
        """
        await db.execute(delete(Kuaile8).where(Kuaile8.id.in_([kuaile8.id])))
